using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.IService.Sys;

namespace net_work.Controllers.Sys;

[Route("api/[controller]")]
[ApiController]
public class AdministrativeClassController(
    ILogger<AdministrativeClassController> logger,
    IAdministrativeClassService administrativeClassService) : ControllerBase
{
    /// <summary>
    /// 获取行政班列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListAdministrativeClasss([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取行政班列表");
        var result = await administrativeClassService.GetAdministrativeClasssPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取行政班信息
    /// </summary>
    /// <param name="administrativeClassId">行政班ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetAdministrativeClassById([FromQuery] long administrativeClassId)
    {
        logger.LogInformation("获取行政班信息");
        return await administrativeClassService.SelectByIdAsync(administrativeClassId) is { } administrativeClass
            ? AjaxResult.Success(administrativeClass)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增行政班
    /// </summary>
    /// <param name="administrativeClassAddRequest">新增行政班实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddAdministrativeClass(
        [FromBody] AdministrativeClassAddRequest administrativeClassAddRequest)
    {
        logger.LogInformation("新增行政班");
        var administrativeClassId =
            await administrativeClassService.InsertAdministrativeClass(administrativeClassAddRequest);
        return administrativeClassId > 0
            ? AjaxResult.Success(new { administrativeClassId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改行政班
    /// </summary>
    /// <param name="administrativeClassUpdateRequest">修改行政班实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateAdministrativeClass(
        [FromBody] AdministrativeClassUpdateRequest administrativeClassUpdateRequest)
    {
        logger.LogInformation("修改行政班");
        return await administrativeClassService.UpdateAdministrativeClass(administrativeClassUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除行政班
    /// </summary>
    /// <param name="ids">行政班ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteAdministrativeClass([FromBody] List<long> ids)
    {
        logger.LogInformation("删除行政班");
        return await administrativeClassService.DeleteAdministrativeClassByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}